package com.ruoyi.notice.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.notice.config.RabbitConfig;
import com.ruoyi.notice.domain.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.notice.mapper.carNoticeMapper;
import com.ruoyi.notice.domain.carNotice;
import com.ruoyi.notice.service.IcarNoticeService;

/**
 * 车联网通知公告Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-12
 */
@Service
public class carNoticeServiceImpl implements IcarNoticeService 
{
    @Autowired
    private carNoticeMapper carNoticeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询车联网通知公告
     * 
     * @param noticeId 车联网通知公告主键
     * @return 车联网通知公告
     */
    @Override
    public carNotice selectcarNoticeByNoticeId(Long noticeId)
    {
        return carNoticeMapper.selectcarNoticeByNoticeId(noticeId);
    }

    /**
     * 查询车联网通知公告列表
     * 
     * @param carNotice 车联网通知公告
     * @return 车联网通知公告
     */
    @Override
    public List<carNotice> selectcarNoticeList(carNotice carNotice)
    {
        return carNoticeMapper.selectcarNoticeList(carNotice);
    }

    /**
     * 新增车联网通知公告
     * 
     * @param carNotice 车联网通知公告
     * @return 结果
     */
    @Override
    public int insertcarNotice(carNotice carNotice)
    {
        carNotice.setCreateTime(DateUtils.getNowDate());

        //获取当前登录用户
        carNotice.setCreateBy(SecurityUtils.getUsername());
        //设置修改人信息
        carNotice.setUpdateBy(SecurityUtils.getUsername());
        //设置修改时间
        carNotice.setUpdateTime(DateUtils.getNowDate());
        //添加通知公告
        int i = carNoticeMapper.insertcarNotice(carNotice);
        //设置通知公告id
        carNotice.setNoticeId(carNotice.getNoticeId());
//        System.out.println(carNotice);
        //判断状态是否是启用
        if(carNotice.getStatus().equals("0")){
            //判断发送全体
            if(carNotice.getNoticePerson().equals("0")){
                //发给用户
                rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"user",carNotice);
            } else if (carNotice.getNoticePerson().equals("1")) {
                //发送给商家
                rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"shop",carNotice);
            }else{
                //发送给全部
                rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"all",carNotice);
            }
        }
        return i;
    }

    /**
     * 修改车联网通知公告
     * 
     * @param carNotice 车联网通知公告
     * @return 结果
     */
    @Override
    public int updatecarNotice(carNotice carNotice)
    {
        carNotice.setUpdateTime(DateUtils.getNowDate());
        //设置修改人信息
        carNotice.setUpdateBy(SecurityUtils.getUsername());
//        carNotice.setNoticeId(carNotice.getNoticeId());
        //判断修改状态是否正常
        if(carNotice.getStatus().equals("0")){
            //判断发送全体
            if(carNotice.getNoticePerson().equals("0")){
                //发给用户
                rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"user",carNotice);
            }else if (carNotice.getNoticePerson().equals("1")) {
                //发送给商家
                rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"shop",carNotice);
            }else{
                //发送给全部
                rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"all",carNotice);
            }
        }else {
            //删除消息
            rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"delete",carNotice);
        }
        return carNoticeMapper.updatecarNotice(carNotice);
    }

    /**
     * 批量删除车联网通知公告
     * 
     * @param noticeIds 需要删除的车联网通知公告主键
     * @return 结果
     */
    @Override
    public int deletecarNoticeByNoticeIds(Long[] noticeIds)
    {
        //构建carNotice对象
        carNotice carNotice = new carNotice();
        for (Long noticeId : noticeIds) {
            carNotice.setNoticeId(noticeId);
            //删除消息
            rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"delete",carNotice);
        }
        return carNoticeMapper.deletecarNoticeByNoticeIds(noticeIds);
    }

    /**
     * 删除车联网通知公告信息
     * 
     * @param noticeId 车联网通知公告主键
     * @return 结果
     */
    @Override
    public int deletecarNoticeByNoticeId(Long noticeId)
    {
        return carNoticeMapper.deletecarNoticeByNoticeId(noticeId);
    }

    /**
     * 商家发布活动
     * @param map
     */
    @Override
    public void addActivity(Map map) {
        //构建carNotice对象
        carNotice carNotice = new carNotice();
        //设置标题
        carNotice.setNoticeTitle(map.get("title").toString());
        //设置内容
        carNotice.setNoticeContent(map.get("content").toString());
        //设置创建人
        carNotice.setCreateBy(map.get("userId").toString());
        //设置创建时间
        carNotice.setCreateTime(DateUtils.getNowDate());
        //设置类型
        carNotice.setNoticeType("1");
        //设置群体
        carNotice.setNoticePerson("0");
        //设置状态
        carNotice.setStatus("2");
        //添加通知公告
        carNoticeMapper.insertcarNotice(carNotice);
    }

    @Override
    public List<carNotice> getActivityByShopUserId(Long shopUserId) {
        List<carNotice> carNoticeList = carNoticeMapper.getActivityByShopUserId(shopUserId);
        System.out.println(carNoticeList);
        return carNoticeList;
    }

    @Override
    public List<Message> getRescueMessageBySid(int sid) {
        List<Message> messageList = carNoticeMapper.getRescueMessageBySid(sid);
        return messageList;
    }
}
